<?php

namespace App\Kuafu\Category\Validate;

use App\Kuafu\Category\Enum\CategoryCodeEnum;
use App\Kuafu\Category\Model\CategoryModel;
use App\Kuafu\Product\Model\Product;
use Illuminate\Http\Request;
use App\Kuafu\Base\Validate\BaseValidate;

class CategoryValidate extends BaseValidate
{
    /**
     * 添加分类校验
     * @param $data
     * @return string
     */
    public static function addOrSetCategory(Request $request): string
    {
        $rules = [
            'name' => [
                'required',
                'max:20',
//                function ($attribute, $value, $fail) use ($request) {
//                    if ($request->method() == "POST") {
//                        if (CategoryModel::where(['name' => $request->name])->first()) {
//                            $fail("分类名称已存在");
//                        }
//                    }
//                }
            ],
//            'desc' => 'required',
//            'imageUrl' => 'required',
            'sort' => 'required|integer|max:255',
        ];
        if ($request->method() == "PUT") {
            $rules['categoryId'] = [
                'required',
                'integer',
                function ($attribute, $value, $fail) use ($request) {
                    if (!CategoryModel::getCategoryInfoById($request->categoryId)) {
                        $fail('分类信息不存在活状态不正确');
                    }
                }
            ];
//            $rules['name'] = [
//                'required',
//                'max:20',
//            ];
            $request['categoryId'] = $request->categoryId;
        }

        $messages = [
            "categoryId.required" => CategoryCodeEnum::CODE_TEXT[CategoryCodeEnum::ERR_CATEGORY_ID_NOT_EXIST],
            "categoryId.integer"  => CategoryCodeEnum::CODE_TEXT[CategoryCodeEnum::ERR_CATEGORY_ID_NOT_EXIST],
            'name.required'       => CategoryCodeEnum::CODE_TEXT[CategoryCodeEnum::ERR_CATEGORY_NAME_NOT_INPUT],
//            'desc.required' => CategoryCodeEnum::CODE_TEXT[CategoryCodeEnum::ERR_CATEGORY_DESC_NOT_INPUT],
            'name.max'            => CategoryCodeEnum::CODE_TEXT[CategoryCodeEnum::ERR_CATEGORY_NAME_TO_LONG],
//            'imageUrl.required' => CategoryCodeEnum::CODE_TEXT[CategoryCodeEnum::ERR_CATEGORY_IMG_NOT_INPUT],
            'sort.integer'        => CategoryCodeEnum::CODE_TEXT[CategoryCodeEnum::ERR_CATEGORY_SORT_NOT_INPUT],
        ];

        return self::validateCheck($request->all(), $rules, $messages);
    }

    /**
     * 删除参数校验
     * @param Request $request
     * @return string
     */
    public static function getCategoryInfoById(Request $request)
    {
        $rules    = [
            'categoryId' => [
                'required',
                'integer',
                function ($attribute, $value, $fail) use ($request) {
                    if (!CategoryModel::getCategoryInfoById($request->categoryId)) {
                        $fail('分类信息不存在活状态不正确');
                    }
                },
                function ($attribute, $value, $fail) use ($request) {
                    if (Product::query()->where(['categoryId' => $request->categoryId])->first()) {
                        $fail('该分类有绑定的菜品，不可删除');
                    }
                }
            ]
        ];
        $messages = [
            "categoryId.required" => CategoryCodeEnum::CODE_TEXT[CategoryCodeEnum::ERR_DATA_NOT_INPUT],
            "categoryId.integer"  => CategoryCodeEnum::CODE_TEXT[CategoryCodeEnum::ERR_DATA_NOT_INPUT],
            "isDeleted.required"  => CategoryCodeEnum::CODE_TEXT[CategoryCodeEnum::ERR_DATA_NOT_INPUT],
        ];
        $data     = [
            'categoryId' => $request->categoryId,
        ];
        return self::validateCheck($data, $rules, $messages);
    }
}
